# -*- coding:utf-8 -*-
'''
Created on Aug 13, 2011

@author: tanfeng
'''

import sys
import urllib2
import re

from autogo.auto.models import *

def get_content(url):
    request = urllib2.Request(url)
    usock = urllib2.urlopen(request)
    page = usock.read().decode('gb2312').encode('utf-8')
    return page

subtitle = ''
def get_title(page):
    global subtitle
    
    re_all_title = r'.*class="bread"(.*?)</span></div>.*'
    re_subtitle = r'.*title="(.*?)".*'
    
    p = re.compile(re_all_title, re.DOTALL)
#    print p
    if p is not None:
        m = p.match(page)
#        print m
        if m is not None:
            titles = m.group(1)
#            print titles
            title_array = titles.split(r'<span style="f13">&raquo;</span>')
            if len(title_array) > 2: 
                used_title = []
                p2 = re.compile(re_subtitle, re.DOTALL)
                for index, t in enumerate(title_array[2:]):
#                    print index, t
                    match = p2.match(t)
                    if match is not None:
                        subtitle = match.group(1)
                        print 'subtitle:',subtitle
                        used_title.append(subtitle)
#            print 'title array:', title_array


mile_headers = []
def get_detail_header(page):
    re_header = r'.*?<thead class="s">.*?保养项目及费用</p></th>(.*?)</thead>.*?'
    
    p = re.compile(re_header, re.DOTALL)
    m = p.match(page)
    if m is not None:
        miles = m.group(1)
#        print 'miles:',miles
        mile_array = miles.split('</th>')
        for m in mile_array:
#            print 'mile:', m
            re_mile = r'.*?>(\d.*?)<br />.*?'
            p2 = re.compile(re_mile, re.DOTALL)
            match = p2.match(m)
            if match is not None:
                mile = match.group(1)
                mile_headers.append(mile)
                print 'header mile:',mile
    print 'mile_headers[2]:', mile_headers[2]

def get_parts(th):
    ''' 获取保养项目的零件 '''
    pass
    
detail_map = {}
def get_detail(page):
    re_detail_table = r'.*?<tbody class="c">(.*?)</tbody>.*?'
    
    p = re.compile(re_detail_table, re.DOTALL)
    m = p.match(page)
    if m is not None:
        all_tr = m.group(1)
        # 分解tr
        tr_array = all_tr.split("<tr>")
        for tr in tr_array:
            # 先分解th, 只获取一次
            re_th = r'.*?<th height="31">(.*?)： <font color="#CB0101">(.*?) </font>元</th>(.*?)</tr>'
            p_th = re.compile(re_th, re.DOTALL)
            m_th = p_th.match(tr)
            if m_th is not None:
#                print m_th.group(1), '-', m_th.group(2), '--', m_th.group(3)
                parts = m_th.group(1)
                price = 0 if m_th.group(2) is None else float(m_th.group(0))
                detail_map[(parts,price)] = None
            
                # 再分解td
                all_td = m_th.group(3).replace('\n','').strip()
                td_array = all_td.split('</td>')
                print 'td array size:', len(td_array)
                for idx,td in enumerate(td_array):
#                    print idx,'---',td,td.find('・')
                    if td.find('・') != -1:
                        mile = 0 if mile_headers[idx] is None else int(mile_headers[idx])
                        detail_map[(parts,price)] = mile
                        print 'care detail:', subtitle, parts, price, mile
                        
                        d = AGCareDetail(car_style_name=subtitle, parts_name=parts, parts_price=price, care_miles=mile)
                        d.save()
        
    print 'map size:',len(detail_map)
    for key in detail_map:
        print 'key:', key[0], key[1], 'val:', detail_map[key]


def parse_care_detail():
    url = 'http://data.auto.sina.com.cn/car_baoyang/subid_%s/'
    for x in xrange(1,1000):
        page = get_content(url % x)
        get_title(page)
        get_detail_header(page)
        get_detail(page)

if __name__ == '__main__':
    
    parse_care_detail()
        
    print 'finished.'